package cn.lger.icollege.service;

import cn.lger.icollege.dao.AttendanceDao;
import cn.lger.icollege.dao.AttendanceMessageDao;
import cn.lger.icollege.dao.ClassRoomDao;
import cn.lger.icollege.dao.StudentDao;
import cn.lger.icollege.entity.*;
import org.springframework.data.domain.Sort;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * Code that Changed the World
 * Pro said
 * Created by Pro on 2018-04-16.
 */
@Service
public class StudentService {

    @Resource(type = StudentDao.class)
    private StudentDao studentDao;
    @Resource
    private AttendanceDao attendanceDao;
    @Resource
    private AttendanceMessageDao attendanceMessageDao;
    @Resource
    private ClassRoomDao classRoomDao;
    @Resource
    private PasswordEncoder encoder;

    public Student findStudentByUsername(String username){
        return studentDao.findStudentByUsername(username);
    }

    @Transactional
    public Student updateStudent(Student student) {
        Student var = studentDao.findStudentByUsername(student.getUsername());
        if (var != null){
            var.setAddress(student.getAddress() == null? var.getAddress() : student.getAddress());
            var.setPhone(student.getPhone() == null? var.getPhone() : student.getPhone());
            if (student.getPassword() != null && !"".equals(student.getPassword().trim()))
                var.setPassword(encoder.encode(student.getPassword()));
            if (student.getIconPath() != null && !"icon/common.jpg".equals(student.getIconPath())){
                var.setIconPath(student.getIconPath());
            }
            return studentDao.save(var);
        }
        return null;
    }


    @Transactional
    public boolean sendMessageToTeacher(String username, String attendanceId, String msg, String filePath) {
        Optional<Attendance> optional = attendanceDao.findById(attendanceId);
        if (optional.isPresent()){
            Attendance attendance = optional.get();
            Teacher teacher = attendance.getTeacher();
            //如果存在消息则消息加1
            teacher.setNews(teacher.getNews() + 1);

            AttendanceMessage message = new AttendanceMessage();
            message.setFromStudent(username);
            message.setToTeacher(teacher.getUsername());
            message.setMsg(msg);
            message.setFilePath(filePath);
            message.setAttendance(attendance);
            //AttendanceMessage作为外键维护需要自己保存，无法通过attendanceDao保存
            attendanceMessageDao.save(message);

            attendanceDao.save(attendance);
            return true;
        }
        return false;
    }

    @Transactional
    public Student changeStudentClassByStudntId(Long classId,String username){
        Student student =studentDao.findStudentByUsername(username);
        if(student!=null){
            Classroom classroom =new Classroom();
            classroom.setClassId(classId);
            student.setClassroom(classroom);
            return  studentDao.save(student);
        }
        return null;
    }

    public Classroom findClassroomByUsername(String username) {
       return classRoomDao.findByStudentUsername(username);
    }

    public Integer getNewsNum(String username) {
        return studentDao.findNewsNumByUsername(username);
    }

    public Student saveStudent(Student student) {
        student.setPassword(encoder.encode(student.getPassword()));
        return studentDao.save(student);
    }

    public void deleteStudent(String username) {
        studentDao.deleteById(username);
    }

    public List<Student> findAll() {
        return studentDao.findAll();
    }

    public List<Student> findAllByClassId(long classId) {
        return studentDao.findAllByClassroomClassId(classId, Sort.by("username"));
    }
}
